package com.zf.zfed.audit.base

import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.zf.zfed.audit.tools.ActivityCollector
import com.zf.zfed.audit.ui.WaitDialog

open class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogUtils.d("当前Activity:", javaClass.simpleName)
        ActivityCollector.addActivity(this)
    }


    fun replaceFragment(containerViewId: Int, fragment: Fragment) {
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(containerViewId, fragment)
        transaction.commit()
    }

    override fun onDestroy() {
        super.onDestroy()
        WaitDialog.hide()
        ActivityCollector.removeActivity(this)
    }


    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        if (event?.keyCode == KeyEvent.KEYCODE_BACK) {
            BarUtils.setStatusBarVisibility(this, false)
            BarUtils.setNavBarVisibility(this, false)
            return true
        }
        return super.dispatchKeyEvent(event)
    }


    /**
     * 显示加载对话框
     */
    open fun showWaitDialog() {
        if (isFinishing || isDestroyed) {
            return
        }
        WaitDialog.show()
    }

    /**
     * 隐藏加载对话框
     */
    open fun hideWaitDialog() {
        if (isFinishing || isDestroyed) {
            return
        }
        WaitDialog.hide()
    }
}